<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/22
 * Time: 17:22
 */
namespace App\Models;
require __DIR__.'/../Library/JPush/autoload.php';
use JPush\Client as JPush;
use Illuminate\Database\Eloquent\Model;
class Send extends Model
{
    public function test($msg){
        $app_key = config('config.JPush.AppKey');
        $master_secret = config('config.JPush.MasterSecret');
        $log_path = null;
        $client = new JPush($app_key, $master_secret, $log_path);
        $push = $client
            ->push()
            ->setPlatform('all')
            ->addAllAudience()
            //->addTag(['tag1', 'tag2']) //bid + token 前六位
            ->setNotificationAlert($msg)
            //->message('Hello JPush')
            ->send();
        if(!empty($push['http_code'])&&$push['http_code']==200){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 极光推送
     *
     * @ author: zuorenci
     * @ date: 2017年9月25日 下午7:55:14
     * @access public|private|protected
     * @param  string  $title       推送的title,会显示在通知栏
     * @param  string  $msg         要推送的消系
     * @param  string  $platform         1 all 2安卓 3IOS
     * @return array        array('status' => 0或是1, 'msg' => '')
     */
    public function sends($title,$msg,$platform){
        if(empty($title)||empty($msg)||empty($platform))
            return false;
        switch ($platform){
            case 1:
                $platform = 'all';
                break;
            case 2:
                $platform = 'android';
                break;
            case 3:
                $platform = 'ios';
                break;
            default:
                return false;
        }
        $app_key = config('config.JPushHui.AppKey');
        $master_secret = config('config.JPushHui.MasterSecret');
        $log_path = null;
        $client = new JPush($app_key, $master_secret, $log_path);
        $push_payload = $client->push()
            ->setPlatform($platform)
            ->addAllAudience()
            ->setNotificationAlert($msg)
            ->iosNotification($msg, array(
                'sound' => 'default',
                'badge' => '+1',
                'content-available' => true,
                'mutable-content'   => true,
            ))
            ->androidNotification($msg, array(
                'title' => $title,
            ))
            ->message($msg, array(
                'title' => $title,
            ));

        try {
            $response = $push_payload->send();
            $msg1['status'] = 1;
            $msg1['msg'] = '推送成功';
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            $msg1['status'] = 0;
            $msg1['msg'] = '推送失败';
        } catch (\JPush\Exceptions\APIRequestException $e) {
            $msg1['msg'] = '推送失败';
        }
        return $msg1;
    }

    /**
     * function name
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/22
     * @param  array   $platform    要推送的设备,类似array('android','ios') 1 all 2android 3 ios
     * @param  array   $alias       要推送的设备别名,别名以用户id规定,array('32+token前六位')
     * @param  string  $title       推送的title,会显示在通知栏
     * @param  string  $msg         要推送的消系
     * @param  string  $build       推送的标识
     * @return array        array('status' => 0或是1, 'msg' => '')
     */
    public function send($platform, $alias, $title='', $msg='', $build='',$arr = []){
        if(empty($platform)||empty($alias))
            return false;
        switch ($platform){
            case 1:
                $platform = 'all';
                break;
            case 2:
                $platform = 'android';
                break;
            case 3:
                $platform = 'ios';
                break;
            default:
                return false;
        }
        $app_key = config('config.JPushHui.AppKey');
        $master_secret = config('config.JPushHui.MasterSecret');
        $log_path = null;
        if(empty($build) || $build == 2){
            $build = '';
        }
        $client = new JPush($app_key, $master_secret, $log_path);
        $push_arr['key'] = $build;
        if (!empty($arr['number'])){
            $push_arr['number'] = $arr['number'];
        }
        $push_payload = $client->push()
            ->setPlatform($platform)
            ->addAlias($alias)
            ->setNotificationAlert($msg)
            ->iosNotification($msg, array(
                'sound' => 'default',
                'badge' => '+1',
                'content-available' => true,
                'mutable-content'   => true,
                'builder_id' => $build,
                'extras' => $push_arr
            ))
            ->androidNotification($msg, array(
                'title' => $title,
                'extras' => $push_arr,
            ))
            ->message($msg, array(
                'title' => $title,
            ));

        try {
            $response = $push_payload->send();
            $msg1['status'] = 1;
            $msg1['msg'] = '推送成功';
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            $msg1['status'] = 0;
            $msg1['error'] = $e;

            $msg1['msg'] = '推送失败1';
        } catch (\JPush\Exceptions\APIRequestException $e) {
            $msg1['status'] = 0;
            $msg1['error'] = $e;
            $msg1['msg'] = '推送失败2';
        }
        return $msg1;
    }


}